<?php
namespace Common\Model;
use Think\Model;

class OrderModel extends Model  {

    protected $tableName = 'b2c_orders';

    public function getOrderMsg($order_id,$member_id){
        $orderMsg = $this->where(['order_id' => $order_id, 'member_id' => $member_id])->find();
        return $orderMsg;
    }

    /**
     * @param $type 情况类型 返回赠送的优惠卷
     * @param $order_id  订单ID
     * @param $store_pid  店铺商品ID
     * $re ：0-接口、非0为函数
     */

    public function isBackCoupon($type,$order_id,$store_pid=0,$re = 0){
        $isSendCoupon = M('b2c_orders')->where(['order_id'=>$order_id])->field('order_promotions,coupon_id')->find();

        if(isset($isSendCoupon)) {
            $order_promotions = unserialize($isSendCoupon['order_promotions']);
            $coupon_id_info = unserialize($isSendCoupon['coupon_id']);
            //print_r($coupon_id_info);
            switch ($type) {
                case 1:
                    //订单全部商品退货退款  返回赠送的优惠卷

                    foreach ($order_promotions['gift_coupon'] as $gk => $gcItem) {
                        $isGiftCoupon = M('b2c_discount_coupon_code')->where(['status' => '0','id' => $gcItem])->save([ 'member_id' => 0,'updated_at'=>date('Y-m-d H:i:s',time())]);
                        //var_dump($isGiftCoupon);exit;
                        if ($isGiftCoupon === false) {
                            if($re){
                                return array('msg' => "返回赠送优惠券失败！", 'status' => 0);
                            }
                            $this->ajaxReturn(array('msg' => "返回赠送优惠券失败！", 'status' => 0));
                        }
                    }
                    //订单全部商品退货退款 返回所有使用的优惠券
                    foreach ($coupon_id_info as $cInfo) {
                        $isOk = M('b2c_discount_coupon_code')->save(['id' => $cInfo['coupon_id'], 'status' => '0','updated_at'=>date('Y-m-d H:i:s',time())]);
                        if ($isOk === false) {
                            if($re){
                                return array('msg' => "返回使用优惠券失败！", 'status' => 0);
                            }
                            $this->ajaxReturn(array('msg' => "返回使用优惠券失败！", 'status' => 0));
                        }
                    }


                    break;
                case 2:
                    //订单部分退款  对应的商品使用该优惠券，退还优惠卷
                    foreach ($coupon_id_info as $cInfo){
                        if($cInfo['type']=='3'){
                            //type = 3 商品优惠券
                            $arr_store_id = explode('#',$cInfo['goods_id_array']);
                            if(in_array($store_pid, $arr_store_id)){
                                $isOk = M('b2c_discount_coupon_code')->save(['id' => $cInfo['coupon_id'], 'status' => '0','updated_at'=>date('Y-m-d H:i:s',time())]);
                                if($isOk === false ){
                                    if($re){
                                        return array('msg' => "返回优惠券失败！", 'status' => 0);
                                    }
                                    $this->ajaxReturn(array('msg' => "返回优惠券失败！", 'status' => 0));
                                }
                            }
                        }
                    }
                    break;
                case 3:
                    //订单未支付，取消订单 返回所有使用的优惠券
                    foreach ($coupon_id_info as $cInfo) {
                        $isOk = M('b2c_discount_coupon_code')->save(['id' => $cInfo['coupon_id'], 'status' => '0','updated_at'=>date('Y-m-d H:i:s',time())]);
                        if ($isOk === false) {
                            if($re){
                                return array('msg' => "返回使用优惠券失败！", 'status' => 0);
                            }
                            $this->ajaxReturn(array('msg' => "返回使用优惠券失败！", 'status' => 0));
                        }
                    }
                    break;
                default:
                    if($re == 0){
                        $this->ajaxReturn(array('msg' => "情况不满足要求", 'status' => 0));
                    }else{
                        return array('msg' => "情况不满足要求", 'status' => 0);
                    }
            }
        }
        if($re == 0){
            $this->ajaxReturn(array('msg' => "成功", 'status' => 1));
        }else{
            return array('msg' => "成功", 'status' => 1);
        }

    }

}